package com.tongzhan.task;

import com.tongzhan.entity.religion.SiteInfo;
import com.tongzhan.entity.religion.Usual;
import com.tongzhan.entity.religion.UsualTask;
import com.tongzhan.service.SiteService;
import com.tongzhan.service.UsualService;
import com.tongzhan.service.UsualTaskService;
import com.tongzhan.utils.DateSwitcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 平安报送任务
 */
@Slf4j
@Component
public class UsualTaskScheduled {

    @Resource
    private SiteService siteService;

    @Resource
    private UsualService usualService;

    @Resource
    private UsualTaskService usualTaskService;

    /**
     * 当天未平安报送的场所生成一条未报送的记录
     */
//  每一分钟cron表达式：0 0/1 * * * ?
//  每天凌晨0点corn表达式：“0 0 0 */1 * ?
    @Scheduled(cron = "0 0 0 */1 * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void createUsual() {
        //受管辖的17处民间信仰点
//        List<String> codes = Arrays.asList("乾元镇-慈相寺", "乾元镇-开封禅寺", "新市镇-千山寺", "钟管镇-范蠡祠", "洛舍镇-赵公祠", "洛舍镇-诸葛禅寺",
//                "雷甸镇-金檀寺", "莫干山镇-万峰庵", "莫干山镇-复兴禅寺", "莫干山镇-余英寺", "莫干山镇-天泉禅寺",
//                "莫干山镇-古铜山寺", "舞阳街道-清莲寺", "阜溪街道-东境寺", "阜溪街道-皇觉寺", "康乾街道-六堡禅寺", "康乾街道-德证寺");
        //查询正在进行的平安报送任务列表
        List<UsualTask> taskList = usualTaskService.selectRunningUsualTask();
        for (UsualTask task : taskList) {
            //报送任务编码
            String usualCode = task.getUsualCode();
            //查询报送记录
            List<Usual> usualList = usualService.selectByUsualCode(usualCode);
            //记录报送场所
            List<String> isReportedList = new ArrayList<>();
            for (Usual usual : usualList) {
                if (!isReportedList.contains(usual.getSiteCode())) {
                    isReportedList.add(usual.getSiteCode());
                }
            }
            //查询未报送场所
            List<SiteInfo> siteList = siteService.selectNotReported(isReportedList);
            //生成未报送记录
            for (SiteInfo siteInfo : siteList) {
                Usual usual = new Usual();
                usual.setSiteCode(siteInfo.getSiteCode());
                usual.setSiteName(siteInfo.getSiteName());
                usual.setStreetCode(siteInfo.getStreetCode());
                usual.setStreetName(siteInfo.getStreetName());
                usual.setReligionCode(siteInfo.getReligionCode());
                usual.setReligionName(siteInfo.getReligionName());
                usual.setUsualCode(task.getUsualCode());
                usual.setUserTitle(task.getTitle());
                usual.setState("未上报");
                usual.setTime(DateSwitcher.getMinTime(LocalDateTime.now().minusDays(1)));
                usualService.insert(usual);
                log.info(siteInfo.getSiteName() + LocalDate.now().minusDays(1).toString() + "未平安报送，生成一条对应为上报记录");
            }
        }
    }

    /**
     * 好的场所为：佛：高峰寺、云岫寺、正道寺、觉海禅寺、永寿寺
     *           道：烟霞观、太玄紫极观、昌福宫、庆云观
     *           基督教：德清教堂、韶村教堂、徐家庄教堂、新市讲堂
     */
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void test() {
//        Map<String, String> map = new HashMap<>();
//        map.put("高峰寺", "1");
//        map.put("云岫寺", "2");
//        map.put("正道寺", "3");
//        map.put("觉海禅寺", "4");
//        map.put("永寿寺", "5");
//        map.put("烟霞观", "6");
//        map.put("太玄紫极观", "7");
//        map.put("昌福宫", "8");
//        map.put("庆云观", "9");
//        map.put("德清教堂", "10");
//        map.put("韶村教堂", "11");
//        map.put("徐家庄教堂", "12");
//        map.put("新市讲堂", "13");
//        //
//        int i = 14;
//        Query query = new Query();
//        query.with(Sort.by(Sort.Direction.ASC, "ReligionCode"));
//        query.addCriteria(Criteria.where("ReligionCode").ne("04"));
//        List<SiteInfo> siteInfos = siteInfoRepository.findAll(query);
//        for (SiteInfo info : siteInfos) {
//            String siteName = info.getSiteName();
//            if (map.containsKey(siteName)) {
//                info.setSort(Integer.valueOf(map.get(siteName)));
//            } else {
//                info.setSort(i);
//                i++;
//            }
//            siteInfoRepository.updateByPrimaryKey(info);
//        }
//        List<SiteInfo> infos = siteInfoRepository.findAll(new Query().addCriteria(Criteria
//                .where("ReligionCode").is("04").and("Jurisdiction").is("是")));
//        for (SiteInfo info : infos) {
//            info.setSort(i);
//            siteInfoRepository.updateByPrimaryKey(info);
//            i++;
//        }
//        List<SiteInfo> ins = siteInfoRepository.findAll(new Query().addCriteria(Criteria
//                .where("ReligionCode").is("04").and("Jurisdiction").is("否")));
//        for (SiteInfo info : ins) {
//            info.setSort(i);
//            siteInfoRepository.updateByPrimaryKey(info);
//            i++;
//        }
//        log.info("----------------SUCCESS-----------------");
//    }
//
//    @Autowired
//    private UserRepository userRepository;
//
//    @Autowired
//    private ModuleSignRepository signRepository;
//
//    @Autowired
//    private StreetRepository streetRepository;
//
//    @Autowired
//    private SiteRepository siteRepository;
//
//    @Autowired
//    private VillageRepository villageRepository;
//
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void test() {
//        List<User> users = userRepository.findAll();
//        for (User user : users) {
//            ModuleSign moduleSign = new ModuleSign();
//            moduleSign.setDutiesCode(user.getDutiesCode());
//            moduleSign.setUserId(user.getUserId());
//            String streetName = user.getStreetName();
//            Street street = streetRepository.findOne(new Query().addCriteria(Criteria.where("StreetName").is(streetName)));
//            if (street == null) {
//                log.info(streetName + "-----------的街道信息为空");
//            } else {
//                user.setStreetCode(street.getStreetCode());
//                moduleSign.setStreetCode(street.getStreetCode());
//            }
//            String villageName = user.getVillageName();
//            if (StringUtils.isNotEmpty(villageName)) {
//                Village village = villageRepository.findOne(new Query().addCriteria(Criteria.where("VillageName").is(villageName)));
//                if (village == null) {
//                    log.info(streetName + villageName + "--------------的村社信息为空");
//                } else {
//                    user.setVillageCode(village.getVillageCode());
//                    moduleSign.setVillageCode(village.getVillageCode());
//                }
//            }
//            String siteName = user.getSiteName();
//            if (StringUtils.isNotEmpty(siteName)) {
//                Site site = siteRepository.findOne(new Query().addCriteria(Criteria.where("SiteName").is(siteName)));
//                if (site == null) {
//                    log.info(streetName + villageName + siteName + "---------------的场所信息为空");
//                } else {
//                    user.setSiteCode(site.getSiteCode());
//                    moduleSign.setSiteCode(site.getSiteCode());
//                }
//            }
//            userRepository.updateByPrimaryKey(user);
//            //模块标识数据
//            moduleSign.setActivity(0L);
//            moduleSign.setBill(0L);
//            moduleSign.setLaw(0L);
//            moduleSign.setLeave(0L);
//            moduleSign.setNotify(0L);
//            moduleSign.setPowerCheck(0L);
//            moduleSign.setSafeCheck(0L);
//            moduleSign.setFireCheck(0L);
//            moduleSign.setUsual(0L);
//            moduleSign.setCondition(0L);
//            moduleSign.setCreateTime(LocalDateTime.now());
//            signRepository.insert(moduleSign);
//        }
//    }
}
